package it.unipd.dei.es1011.nucularc0d3.defaultproject;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
/**
 * @version 3.14159265
 * @author darkfrank
 *
 */
public class Graphizer extends Activity{
	private DrawView drawView = null;
	String fileName = "";

	ProgressDialog mDialog = null;

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/** full screen, no title*/
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		/** show a progress dialog until the graph are showed */
		mDialog = ProgressDialog.show(Graphizer.this,
				"Please wait...", "Doing Extreme Calculations...", true);

		/** the filename of the file xml to load */
		fileName = getIntent().getExtras().getString("fileName");

		/** create the View that make the graph, we pass the the reference to this
		 * class, the fileName (see above), the width and the height of the screen */
		drawView = new DrawView(Graphizer.this, fileName, getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight());

		/** show the view */
		setContentView(drawView);
	}

	/**
	 * This class is used to delete the progress dialog showed at the beginning of
	 * the activity.
	 */
	public void cancelDialog(){
		if(mDialog != null){
			mDialog.cancel();
			mDialog = null;
		}
	}	

	/** When activity are resumed tell the view to wake up and draw itself. */
	@Override
	protected void onResume() {
		super.onResume();
		/** show short instructions */
		Toast.makeText(getApplicationContext(), 
				getResources().getString(R.string.grap_s_help), 
				Toast.LENGTH_LONG).show();
		drawView.onResume();
	}

	/**
	 * This create the menu from the xml, if some axes are missing
	 * then the their icon are removed.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.graphizermenu, menu);
		if(!drawView.isX())
			menu.removeItem(R.id.togglex);
		if(!drawView.isY())
			menu.removeItem(R.id.toggley);
		if(!drawView.isZ())
			menu.removeItem(R.id.togglez);
		return true;
	}

	/**
	 * Action of the menu icon.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.icontext: 
			// Start a new Activity that show the details of this session.
			Intent intent = new Intent(this, Detail.class);
			intent.putExtra("fileName", fileName);
			startActivity(intent);
			break;
		case R.id.togglex:
			// hide or reveals the X graph 
			drawView.toggleX();
			break;	
		case R.id.toggley:
			// hide or reveals the Y graph
			drawView.toggleY();
			break;
		case R.id.togglez:
			// hide or reveals the Z graph
			drawView.toggleZ();
			break;
		case R.id.help:
			// show Message of help
			Dialog dialog = new Dialog(Graphizer.this);
			dialog.setContentView(R.layout.graphelp);
			dialog.setTitle("Help using Chart.");
			dialog.setCancelable(true);
			dialog.setCanceledOnTouchOutside(true);
			dialog.show();
		}
		return true;
	}
}